Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDownloading:false,
    percentNum:0,
    bookList:[
      {
        id:'001',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"Java编程思想",
        fileUrl:"http://172.20.100.9:8888/%E9%AB%98%E6%A2%A8%E5%A8%9C%E8%B3%87%E6%96%99/%E6%BB%91%E9%BC%A0/AMMOX%20X1-400/AMMOX%20X1-400%20CTN%2012101644100-A%20.PDF"
      },
      {
        id:'002',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"C#高级编程(第7版)",
        fileUrl:"http://172.20.100.9:8888/%E5%B7%A5%E5%96%AESOP/study/C%23%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%28%E7%AC%AC7%E7%89%88%29.pdf"
      },
      {
        id:'003',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"python从入门到入土",
        fileUrl:"http://172.20.100.9:8888/%E9%AB%98%E6%A2%A8%E5%A8%9C%E8%B3%87%E6%96%99/%E6%BB%91%E9%BC%A0/AMMOX%20X1-400/AMMOX%20X1-400%20CTN%2012101644100-A%20.PDF"
      },
      {
        id:'004',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"Java编程思想",
        fileUrl:"http://172.20.100.9:8888/%E9%AB%98%E6%A2%A8%E5%A8%9C%E8%B3%87%E6%96%99/%E6%BB%91%E9%BC%A0/AMMOX%20X1-400/AMMOX%20X1-400%20CTN%2012101644100-A%20.PDF"
      },
      {
        id:'005',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"Java编程思想",
        fileUrl:"http://172.20.100.9:8888/%E9%AB%98%E6%A2%A8%E5%A8%9C%E8%B3%87%E6%96%99/%E6%BB%91%E9%BC%A0/AMMOX%20X1-400/AMMOX%20X1-400%20CTN%2012101644100-A%20.PDF"
      },
      {
        id:'006',
        poster:"https://img3.doubanio.com/view/subject/1/public/s27243455.jpg",
        title:"Java编程思想",
        fileUrl:"http://172.20.100.9:8888/%E9%AB%98%E6%A2%A8%E5%A8%9C%E8%B3%87%E6%96%99/%E6%BB%91%E9%BC%A0/AMMOX%20X1-400/AMMOX%20X1-400%20CTN%2012101644100-A%20.PDF"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 封装showModal方法
   */
showTips(content){
  wx.showModal({
    title: '提醒',
    content:content,
    showCancel:false
  })
},
/**
 * 封装打开图书方法
 */
openBook(path){
  wx.openDocument({
    filePath: path,
    success:(res)=>{
      console.log('打开图书成功');
    },
    fail:(error)=>{
      console.log(error);
    }
  })
},

/**
 * 保存图书的方法
 */
saveBook(id,path){
  var that=this
  wx.saveFile({
    tempFilePath: path,
    success:(res)=>{
      //将文件保存到本地缓存中,下次直接打开
      let newPath=res.savedFilePath
      wx.setStorageSync(id, newPath)
      //打开图书
      that.openBook(newPath)
    },
    fail:(error)=>{
      console.log(error);
      this.showTips('文件保存失败!')
    }
  })
},

/**
 * 阅读图书
 */

 readBook(e){
  var that=this
  // 获取当前图书的id
  let id = e.currentTarget.dataset.id
  // 获取当前url地址
  let fileUrl= e.currentTarget.dataset.file

  //先查看本地缓存
  let path=wx.getStorageSync(id)

  //如果缓存为空
  if(path==''){
    // 切换到下载时的蒙层
    that.setData({
      isDownloading:true
    })
    //接着下载
    const downloadTask=wx.downloadFile({
      url: fileUrl,
      success:(res)=>{
        that.setData({
          isDownloading:false
        })
        //如果下载成功
        if(res.statusCode==200){
          path=res.tempFilePath
          that.saveBook(id,path)
        }else{
          that.showTips('暂时无法下载')
        }
      },
      fail:(e)=>{
        that.setData({
          isDownloading:false
        })
        that.showTips('无法连接到服务器')
      }
    })

    // 监听当前文件的下载进度
    downloadTask.onProgressUpdate((res)=>{
      let progress=res.progress
      that.setData({
        percentNum:progress
      })
    })

  }else{  //如果不为空直接打开
    that.openBook(path)
  }


  console.log(e);
 },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})